/**
 * Created on Nov 24, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

package com.eibus.applicationconnector.ejb;
import junit.framework.TestCase;
import com.eibus.connector.nom.*;

import com.eibus.xml.nom.Node;
import com.eibus.xml.xpath.*;


/**
 * @author cabraham
 *
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class EjbTestCase extends TestCase {

	Connector connector;

	/*public static void main(String[] args) {
		junit.textui.TestRunner.run(EjbTestCase.class);

	}*/

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp(){
		try
		{
			super.setUp();
			connector = Connector.getInstance("Ejb Connector");
			connector.open();
		}
		catch(Exception e)
		{
			fail("Exception in setup(). Create/open connector.");
			e.printStackTrace();
		}
	}

	/*
	 * @see TestCase#tearDown()
	 */
	protected void tearDown(){
		try
		{
			super.tearDown();
			connector.close();
		}
		catch(Exception e)
		{
			fail("Exception in tearDown()."+e.toString());
		}
	}


	public  boolean checkforSoapFault(int response) throws Exception
	{
		int resultNode=0;
		try
		{
			//int  faultNode =Find.firstMatch(response,"?<SOAP:Fault>");
			XPathMetaInfo metaInfo = new XPathMetaInfo();
			metaInfo.addNamespaceBinding("soap","http://schemas.xmlsoap.org/soap/envelope/");
			XPath faultXpath = new XPath("descendant-or-self::soap:Fault");
			int faultNode = faultXpath.firstMatch(response,metaInfo);

			if ( faultNode != 0)
			{
				fail("EJB Call Method failed with SOAP Fault :-"+Node.writeToString(faultNode,true));
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}


	public void testGetSessionBeans()
	{
		int request=0,response =0;
		try
		{
			request = connector.createSOAPMethod("http://schemas.cordys.com/1.0/metadata/ejb","getSessionBeans");
			request=Node.getRoot(request);
			response=connector.sendAndWait(request);
			checkforSoapFault(response);
			XPath respOldXpath = new XPath("descendant-or-self::getSessionBeansResponse/tuple/old");
			//int tuple=Find.firstMatch(response,"?<getSessionBeansResponse><tuple><old>");
			int tuple = respOldXpath.firstMatch(response,null);
			if(tuple == 0)
				fail("EJB Call GetSessionBean Method Returned Empty Response");
		}
		catch(Exception ex)
		{
			fail("EJB Call GetSessionBeans Method failed with Exception" +ex.toString());
		}
		finally
		{
			Util.delete(request);
			Util.delete(Node.getRoot(response));
		}
	}

	public void testGetEntityBeans()
	{
		int request=0,response =0;
		try
		{
			request = connector.createSOAPMethod("http://schemas.cordys.com/1.0/metadata/ejb","getEntityBeans");
			request=Node.getRoot(request);
			response=connector.sendAndWait(request);
			checkforSoapFault(response);
			XPath entityrespXpath = new XPath("descendant-or-self::getEntityBeansResponse/tuple/old");
			//int tuple=Find.firstMatch(response,"?<getEntityBeansResponse><tuple><old>");
			int tuple = entityrespXpath.firstMatch(response,null);
			if(tuple == 0)
				fail("EJB Call getEntityBeans Method Returned Empty Response");

		}
		catch(Exception ex)
		{
			fail("EJB Call GetEntityBeans Method failed with Exception" +ex.toString());
		}
		finally
		{
			Util.delete(request);
			Util.delete(Node.getRoot(response));
		}
	}

	public void testGetBeanInfo()
	{
		int request=0,response =0;
		try
		{
			request = connector.createSOAPMethod("http://schemas.cordys.com/1.0/metadata/ejb","getBeanInfo");
			Node.createElementWithParentNS("beanname","calculatorTest",request);
			request=Node.getRoot(request);
			response=connector.sendAndWait(request);
			checkforSoapFault(response);
			XPath beanInfoXpath = new XPath("descendant-or-self::getBeanInfoResponse/tuple/old");
			//int tuple=Find.firstMatch(response,"?<getBeanInfoResponse><tuple><old>");
			int tuple = beanInfoXpath.firstMatch(response,null);
			if(tuple == 0)
				fail("EJB Call GetBeanInfo Method Returned Empty Response");

		}
		catch(Exception ex)
		{
			fail("EJB Call GetBeanInfo Method failed with Exception" +ex.toString());
		}
		finally
		{
			Util.delete(request);
			Util.delete(Node.getRoot(response));
		}
	}

	public void testGetInterfaceInfo()
	{
		int request=0,response =0;
		try
		{
			request = connector.createSOAPMethod("http://schemas.cordys.com/1.0/metadata/ejb","getInterfaceInfo");
			Node.createElementWithParentNS("interfacename","examples.ejb20.basic.test.Calculator",request);
			Node.createElementWithParentNS("beanname","calculatorTest",request);
			request=Node.getRoot(request);
			response=connector.sendAndWait(request);
			checkforSoapFault(response);
			XPath interfaceXpath = new XPath("descendant-or-self::getInterfaceInfoResponse/tuple/old");
			//int tuple=Find.firstMatch(response,"?<getInterfaceInfoResponse><tuple><old>");
			int tuple = interfaceXpath.firstMatch(response,null);
			if(tuple == 0)
				fail("EJB Call GetInterfaceInfo Method Returned Empty Response");

		}
		catch(Exception ex)
		{
			fail("EJB Call GetInterfaceInfo Method failed with Exception" +ex.toString());
		}
		finally
		{
			Util.delete(request);
			Util.delete(Node.getRoot(response));
		}
	}
}
